import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UdpServe2 {
    private DatagramSocket socket=null;
    public UdpServe2(int port) throws SocketException {
        socket=new DatagramSocket(port);
    }
    public void start() throws IOException {
        System.out.println("服务器启动");
        while(true)
        {
            DatagramPacket packet=new DatagramPacket(new byte[5000],5000);
            socket.receive(packet);
            String request=new String(packet.getData(),0, packet.getLength());
            String response=process(request);
            DatagramPacket datagramPacket=new DatagramPacket(response.getBytes(),response.getBytes().length,
                    packet.getSocketAddress());
            socket.send(datagramPacket);
            System.out.printf("[%s:%d]req:%s;resp:%s",packet.getAddress().toString(),packet.getPort(),request,
                    response);
        }
    }
    public String process(String request)
    {
        return request;
    }

    public static void main(String[] args) throws IOException {
        UdpServe2 serve2=new UdpServe2(10002);
        serve2.start();

    }
}
